﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
using DBUtility;


namespace BLL.Common
{
    /// <summary>
    /// 业务逻辑类ResourceStatus 的摘要说明。
    /// </summary>
    public class ResourceStatus
    {
        private readonly OracleDAL.Common.ResourceStatus dal = new OracleDAL.Common.ResourceStatus();
        /// <summary>
        /// 构造函数
        /// </summary>
        public ResourceStatus()
        { }


        #region 成员
        /// <summary>
        /// 获取设备列表
        /// </summary>
        /// <param name="AREAID">区域ID</param>
        /// <param name="ROOMID">机房ID</param>
        /// <param name="EQUIPID">设备ID</param>
        /// <returns></returns>
        public DataSet GetEquipList(string AREAID, string ROOMID, string EQUIPID)
        {
            return dal.GetEquipList(AREAID, ROOMID, EQUIPID);
        }
        /// <summary>
        /// 得到资源状态实例
        /// </summary>
        /// <param name="SUBSYSTEMID">子系统ID</param>
        /// <param name="RESOURCETYPEID">资源类型ID</param>
        /// <param name="RESOURCEID">资源ID</param>
        /// <returns></returns>
        public Model.Common.ResourceStatus GetModel(string SUBSYSTEMID, string RESOURCETYPEID, string RESOURCEID)
        {
            return dal.GetModel(SUBSYSTEMID, RESOURCETYPEID, RESOURCEID);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Model.Common.ResourceStatus> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获取资源状态数据集
        /// </summary>
        /// <param name="SUBSYSTEMID">子系统ID</param>
        /// <param name="RESOURCETYPEID">资源类型ID</param>
        /// <param name="TYPE">资源类型</param>
        /// <returns></returns>
        public List<Model.Common.ResourceStatus> GetModelList(string SUBSYSTEMID, string RESOURCETYPEID, string TYPE)
        {
            DataSet ds = dal.GetList(SUBSYSTEMID, RESOURCETYPEID, TYPE);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Model.Common.ResourceStatus> DataTableToList(DataTable dt)
        {
            List<Model.Common.ResourceStatus> modelList = new List<Model.Common.ResourceStatus>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.Common.ResourceStatus model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Model.Common.ResourceStatus();
                    model.RESOURCEID = dt.Rows[n]["RESOURCEID"].ToString();
                    model.RESOURCENAME = dt.Rows[n]["RESOURCENAME"].ToString();
                    model.RESOURCESTATUS = dt.Rows[n]["RESOURCESTATUS"].ToString();
                    model.RESOURCETYPEID = dt.Rows[n]["RESOURCETYPEID"].ToString();
                    model.RESOURCETYPENAME = dt.Rows[n]["RESOURCETYPENAME"].ToString();
                    model.SUBSYSTEMID = dt.Rows[n]["SUBSYSTEMID"].ToString();
                    modelList.Add(model);
                }
            }
            return modelList;
        }
        /// <summary>
        /// 获取设备各类型的数目
        /// </summary>
        /// <param name="EQUIPID">上级设备ID</param>
        /// <returns></returns>
        public DataSet GetTypeCount(string EQUIPID)
        {
            return dal.GetTypeCount(EQUIPID);
        }
        /// <summary>
        /// 获取资源类型的基本信息
        /// </summary>
        /// <param name="TYPEID">资源类型ID</param>
        /// <returns></returns>
        public DataSet GetResourceTypeInfo(string TYPEID)
        {
            return dal.GetResourceTypeInfo(TYPEID);
        }
        #endregion

        #region 获取设备属性字段
        /// <summary>
        /// 获取类型属性字段
        /// </summary>
        /// <param name="TYPEID">类型ID</param>
        /// <returns></returns>
        public DataSet GetColumnesList(string TYPEID)
        {
            return dal.GetColumnesList(TYPEID);
        }
        /// <summary>
        /// 获取类型属性字段
        /// </summary>
        /// <param name="TABLENAME">资源对应的表明</param>
        /// <param name="ISVISIBLE">字段是否显示(为空显示所有,N查询不显示,Y查询显示字段)</param>
        /// <returns></returns>
        public DataSet GetColumnesList(string TABLENAME, string ISVISIBLE)
        {
            return dal.GetColumnesList(TABLENAME, ISVISIBLE);
        }
        /// <summary>
        /// 删除逻辑设备
        /// </summary>
        /// <param name="LOGICEQUIPMENTID">逻辑设备ID</param>
        /// <returns></returns>
        public int Delete(string LOGICEQUIPMENTID)
        {
            return dal.Delete(LOGICEQUIPMENTID);
        }
        /// <summary>
        /// 获取资源页签
        /// </summary>
        /// <param name="TYPEID">类型ID</param>
        /// <returns></returns>
        public DataSet GetResourceTab(string TYPEID)
        {
            return dal.GetResourceTab(TYPEID);
        }
        /// <summary>
        /// 获取资源值(属性或性能)
        /// </summary>
        /// <param name="TABLENAME">查询资源表名</param>
        /// <param name="RESOURCETYPEID">查询资源ID(如果资源类型ID没有，就直接使用表明，如果资源类型ID存在就查询相应的视图)</param>
        /// <param name="TYPE">类型(属性、性能)</param>
        /// <param name="SORT">排序字段</param>
        /// <param name="strWhere">查询条件</param>
        /// <returns></returns>
        public DataSet GetResourceValue(string TABLENAME, string RESOURCETYPEID, string TYPE, string SORT, string strWhere)
        {
            return dal.GetResourceValue(TABLENAME, RESOURCETYPEID, TYPE, SORT, strWhere);
        }
        /// <summary>
        /// 获取IT设备基本指标数据
        /// </summary>
        /// <param name="TABLENAME">表名</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="SORT">排序</param>
        /// <returns></returns>
        public DataSet GetIndicatorValue(string TABLENAME, string strWhere, string SORT)
        {
            return dal.GetIndicatorValue(TABLENAME, strWhere, SORT);
        }
        /// <summary>
        /// 获取设备属性名称
        /// </summary>
        /// <param name="TABLENAME">表名</param>
        /// <param name="RESOURCETYPEID">资源类型ID</param>
        /// <param name="TYPE">类型</param>
        /// <param name="SORT">排序字段</param>
        /// <param name="strWhere">查询条件</param>
        /// <returns></returns>
        public DataSet GetEquipProperty(string TABLENAME, string RESOURCETYPEID, string TYPE, string SORT, string strWhere)
        {
            return dal.GetEquipProperty(TABLENAME, RESOURCETYPEID, TYPE, SORT, strWhere);
        }
        /// <summary>
        /// 获取LOOP设备是否在网
        /// </summary>
        /// <returns></returns>
        public DataSet GetLoopIsInnet()
        {
            return dal.GetLoopIsInnet();
        }

        /// <summary>
        /// 将DataTable转化为JSON字符串形式,标记为表格的字段名称
        /// </summary>
        /// <param name="dt">DataTable</param>
        /// <returns></returns>
        public string GetTableToJSON(DataTable dt)
        {
            StringBuilder strJSON = new StringBuilder();
            string colName = string.Empty;
            object colValue = new object();
            if (dt != null && dt.Rows.Count > 0)
            {
                strJSON.Append("[");
                int length = dt.Rows.Count;
                for (int i = 0; i < length; i++)
                {
                    strJSON.Append("{");
                    int count = dt.Columns.Count;
                    for (int j = 0; j < count; j++)
                    {
                        colName = dt.Columns[j].ColumnName.ToString();
                        colValue = dt.Rows[i][colName];
                        strJSON.Append("\"" + colName + "\":\"" + (colValue == null ? "" : colValue.ToString()) + "\"");
                        //strJSON.Append(colName + ":'" + (colValue == null ? "" : colValue.ToString()) + "'");
                        if (j < count - 1)
                        {
                            strJSON.Append(",");
                        }
                    }
                    strJSON.Append("}");
                    if (i < length - 1)
                    {
                        strJSON.Append(",");
                    }
                }
                strJSON.Append("]");
                return strJSON.ToString();
            }
            return string.Empty;
        }
        /// <summary>
        /// 将DataTable转化为JSON字符串形式,标记为表格的字段名称
        /// </summary>
        /// <param name="dt">DataTable</param>
        /// <param name="filter">过滤字段</param>
        /// <returns></returns>
        public string GetTableToJSON(DataTable dt, string filter)
        {
            StringBuilder strJSON = new StringBuilder();
            string colName = string.Empty;
            object colValue = new object();
            if (dt != null && dt.Rows.Count > 0)
            {
                DataTable dtNew = new DataSetHelper().Distinct("", dt, filter);
                int dtLength = dtNew.Rows.Count;
                for (int n = 0; n < dtLength; n++)
                {
                    strJSON.Append("\"" + dtNew.Rows[n][filter].ToString() + "s\":[");
                    DataRow[] dr = dt.Select(filter + "='" + dtNew.Rows[n][filter].ToString() + "'");
                    int length = dr.Length;
                    for (int i = 0; i < length; i++)
                    {
                        strJSON.Append("{");
                        int count = dt.Columns.Count;
                        for (int j = 0; j < count; j++)
                        {
                            colName = dt.Columns[j].ColumnName.ToString();
                            colValue = dr[i][colName];
                            strJSON.Append("\"" + colName + "\":\"" + (colValue == null ? "" : colValue.ToString().Replace('"', '\'')) + "\"");
                            //strJSON.Append(colName + ":'" + (colValue == null ? "" : colValue.ToString()) + "'");
                            if (j < count - 1)
                            {
                                strJSON.Append(",");
                            }
                        }
                        strJSON.Append("}");
                        if (i < length - 1)
                        {
                            strJSON.Append(",");
                        }
                    }
                    strJSON.Append("]");
                    if (n < dtLength - 1)
                    {
                        strJSON.Append(",");
                    }
                }
                return strJSON.ToString();
            }
            return string.Empty;
        }
        /// <summary>
        /// 将将对象转化为JSON字符串形式
        /// </summary>
        /// <param name="obj">需要转化的对象</param>
        /// <returns></returns>
        public string ObjectToJSON(object obj)
        {
            StringBuilder strJSON = new StringBuilder();
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
            MemoryStream stream = new MemoryStream();
            serializer.WriteObject(stream, obj);
            byte[] dataBytes = new byte[stream.Length];
            stream.Position = 0;
            stream.Read(dataBytes, 0, (int)stream.Length);
            return Encoding.UTF8.GetString(dataBytes);
        }
        /// <summary>
        /// 将JSON字符串转化成相应的对象
        /// </summary>
        /// <param name="jsonString">JSON字符串</param>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public object JSONToObject(string jsonString, object obj)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
            MemoryStream mStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            return serializer.ReadObject(mStream);
        }
        /// <summary>
        /// 格式化JSON字符串(去除特殊字符)
        /// </summary>
        /// <param name="s">JSON字符串</param>
        /// <returns></returns>
        public string ToJson(string s)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < s.Length; i++)
            {
                char c = s.ToCharArray()[i];
                switch (c)
                {
                    case '\"': sb.Append("\\\""); break;
                    case '\\': sb.Append("\\\\"); break;
                    case '/': sb.Append("\\/"); break;
                    case '\b': sb.Append("\\b"); break;
                    case '\f': sb.Append("\\f"); break;
                    case '\n': sb.Append("\\n"); break;
                    case '\r': sb.Append("\\r"); break;
                    case '\t': sb.Append("\\t"); break;
                    default: sb.Append(c); break;
                }
            }
            return sb.ToString();
        }
        /// <summary>
        /// 获取机房的所有资源
        /// </summary>
        /// <param name="ROOMID">机房ID</param>
        /// <returns></returns>
        public DataSet GetRoomEquip(string ROOMID)
        {
            return dal.GetRoomEquip(ROOMID);
        }
        /// <summary>
        /// 获取设备ID对象下的设备列表
        /// </summary>
        /// <param name="EQUIPID">设备ID</param>
        /// <param name="EQUIPTYPE">设备类型</param>
        /// <returns></returns>
        public DataSet GetEquip(string EQUIPID, string EQUIPTYPE)
        {
            return dal.GetEquip(EQUIPID, EQUIPTYPE);
        }
        #endregion
        /// <summary>
        /// 查询表
        /// </summary>
        /// <param name="TABLENAME">表名</param>
        /// <param name="strWhere">查询条件</param>
        /// <returns></returns>
        public DataSet GetResourceType(string TABLENAME, string strWhere)
        {
            return dal.GetResourceType(TABLENAME, strWhere);
        }
    }
}